package com.eorchis.util.excel;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Title: ExcelRules<br>
 * Description: Excel导入规则类<br>
 * Copyright @ 2011~2015 Goldgov .All rights reserved.<br>
 * 
 * @author LuanKW
 * @createDate 2015-5-12
 * @version $Revision: $
 */
public class ExcelRules {
	
	private Class<?> fieldType; //属性类型
	
	private String errorMessage; //异常记录

	private boolean nullAble; //是否可以为空
	
	private int maxLength; //最大长度
	
	private String dateFormat; //日期转换格式

	public ExcelRules(Class<?> fieldType,String errorMessage,boolean nullAble,int maxLength,String dateFormat){
		this.fieldType = fieldType;
		this.errorMessage = errorMessage;
		this.nullAble = nullAble;
		this.maxLength = maxLength;
		this.dateFormat = dateFormat;
	}
	
	/**
	 * 校验Excel文件中的所有信息，错误结果统一展示
	 * 
	 * @param cellValue
	 * @param excelRules
	 * @param excelBean
	 * @return
	 * @throws Exception
	 */
	public static boolean check(String cellValue, ExcelRules excelRules, int rowIndex, int cellIndex, ExcelBean excelBean) throws Exception{
		String customErrorMessage = excelRules.getErrorMessage();
		//String custom
		// 空值校验
		if(!excelRules.isNullAble()&&(cellValue==null||"".equals(cellValue))){
			if(customErrorMessage!=null&&!"".equals(customErrorMessage)){
				excelBean.getErrorMessage().append((rowIndex+1)+"行,"+(cellIndex+1)+"列："+customErrorMessage+excelBean.errorMessageSplit());
			}else{
				excelBean.getErrorMessage().append((rowIndex+1)+"行,"+(cellIndex+1)+"列：此单元格内容不能为空值"+excelBean.errorMessageSplit());
			}
			return false;
		}
		//非空情况下进行以下属性类型校验
		if(cellValue!=null&&!"".equals(cellValue)){
			// 数值形
			if(excelRules.getFieldType() == Integer.class||excelRules.getFieldType() == Double.class){
				try{
					// 此处采用浮点数转换，验证value中是否含有字符。整形转换时只要整数部分的数据
					Double.parseDouble(cellValue);
				}catch(NumberFormatException ne){
					if(customErrorMessage!=null&&!"".equals(customErrorMessage)){
						excelBean.getErrorMessage().append((rowIndex+1)+"行,"+(cellIndex+1)+"列："+customErrorMessage+excelBean.errorMessageSplit());
					}else{
						excelBean.getErrorMessage().append((rowIndex+1)+"行,"+(cellIndex+1)+"列：此单元格内容只能为数值，读取时为："+cellValue+excelBean.errorMessageSplit());
					}
					return false;
				}
			}
			// 日期型
			else if(excelRules.getFieldType() == Date.class){
				String dateFormat = excelRules.getDateFormat();
				SimpleDateFormat sf = new SimpleDateFormat(dateFormat);
				try{
					sf.parse(cellValue);
				}catch(ParseException e){
					if(customErrorMessage!=null&&!"".equals(customErrorMessage)){
						excelBean.getErrorMessage().append((rowIndex+1)+"行,"+(cellIndex+1)+"列：读取时为："+cellValue+customErrorMessage+excelBean.errorMessageSplit());
					}else{
						excelBean.getErrorMessage().append((rowIndex+1)+"行,"+(cellIndex+1)+"列：此单元格内容中日期格式异常，读取时为："+cellValue+" 正确格式应为："+dateFormat+excelBean.errorMessageSplit());
					}
					return false;
				}
			}
			// 长度校验
			if(excelRules.getMaxLength()!=-1&&cellValue.length()>excelRules.getMaxLength()){
				if(customErrorMessage!=null&&!"".equals(customErrorMessage)){
					excelBean.getErrorMessage().append((rowIndex+1)+"行,"+(cellIndex+1)+"列："+customErrorMessage+excelBean.errorMessageSplit());
				}else{
					excelBean.getErrorMessage().append((rowIndex+1)+"行,"+(cellIndex+1)+"列：此单元格内容的长度不能超出"+excelRules.getMaxLength()+"个字符"+excelBean.errorMessageSplit());
				}
				return false;
			}
		}
		return true;
	}

	/**
	 * @return the nullAble
	 */
	public boolean isNullAble() {
		return nullAble;
	}

	/**
	 * @param nullAble the nullAble to set
	 */
	public void setNullAble(boolean nullAble) {
		this.nullAble = nullAble;
	}

	/**
	 * @return the maxLength
	 */
	public int getMaxLength() {
		return maxLength;
	}

	/**
	 * @param maxLength the maxLength to set
	 */
	public void setMaxLength(int maxLength) {
		this.maxLength = maxLength;
	}

	/**
	 * @return the errorMessage
	 */
	public String getErrorMessage() {
		return errorMessage;
	}

	/**
	 * @param errorMessage the errorMessage to set
	 */
	public void setErrorMessage(String errorMessage) {
		this.errorMessage = errorMessage;
	}

	/**
	 * @return the fieldType
	 */
	public Class<?> getFieldType() {
		return fieldType;
	}

	/**
	 * @param fieldType the fieldType to set
	 */
	public void setFieldType(Class<?> fieldType) {
		this.fieldType = fieldType;
	}

	/**
	 * @return the dateFormat
	 */
	public String getDateFormat() {
		return dateFormat;
	}

	/**
	 * @param dateFormat the dateFormat to set
	 */
	public void setDateFormat(String dateFormat) {
		this.dateFormat = dateFormat;
	}
	
}
